package com.haoyeteng.sxbd.modular.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.constant.SxbdConstants;
import com.haoyeteng.sxbd.core.exception.SxbdException;
import com.haoyeteng.sxbd.core.shiro.ShiroAdmin;
import com.haoyeteng.sxbd.core.shiro.ShiroKit;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.model.*;
import com.haoyeteng.sxbd.modular.business.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 总览信息
 *
 * @author zhanglixiang
 * @Date 2017年3月4日23:05:54
 */
@Controller
@RequestMapping("/blackboard")
public class BlackboardController extends BaseController {

	@Autowired
	private IUserService userService;
	
	@Autowired
	private IAuthService authService;
	
	@Autowired
	private IAccountService accountService;

	@Autowired
	private IOrderService orderService;

	@Autowired
	private IBillService billService;

	@Autowired
	private IWarnService warnService;

    
    /**
     * 跳转到黑板
     */
    @RequestMapping("")
    public String blackboard(Model model) {
		ShiroAdmin admin = ShiroKit.getAdmin();
		model.addAttribute("admin", admin);
		int userCount = userService.selectCount(new EntityWrapper<User>());
		model.addAttribute("userCount", userCount);
		int authCount = authService.selectCount(new EntityWrapper<Auth>().eq("auth_state", 1));
		model.addAttribute("authCount", authCount);
		/*Calendar cal = Calendar.getInstance();
		int thatYear = cal.get(Calendar.YEAR);
		model.addAttribute("thatYear", thatYear);
		int lastYear = thatYear - 1;
		model.addAttribute("lastYear", lastYear);
		int beforeYear = thatYear - 2;
		model.addAttribute("beforeYear", beforeYear);*/

		//报警数
		//List<Map<String, Object>> warns = warnService.getWarns(null,null);

		//统计服务
		int rtdCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "RTD"));
		//待处理rtd 未绑定,且系统标识为空
		int toDoRtdCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "RTD")
				.isNull("system_sign").eq("binding_state", SxbdConstants.ACCOUNT_BINDING_STATE_NO));

		int rtkCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "RTK"));
		//待处理rtk 未绑定,且系统标识为空
		int toDoRtkCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "RTK")
				.isNull("system_sign").eq("binding_state", SxbdConstants.ACCOUNT_BINDING_STATE_NO));
		int zbzhCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "ZBZH"));
		int jzsjCount = accountService.selectCount(new EntityWrapper<Account>().eq("service_type", "JZSJ"));

		model.addAttribute("rtdCount", rtdCount);
		model.addAttribute("toDoRtdCount", toDoRtdCount);
		model.addAttribute("rtkCount", rtkCount);
		model.addAttribute("toDoRtkCount", toDoRtkCount);
		model.addAttribute("zbzhCount", zbzhCount);
		model.addAttribute("jzsjCount", jzsjCount);

		//订单服务
		List<Order> orderList = orderService.selectList(new EntityWrapper<Order>()
				.eq("order_state", SxbdConstants.ORDER_STATE_NEW)
				.eq("pay_state",SxbdConstants.ORDER_PAY_STATE_PAYMENTED));
		//订单总数(已完成)
		Integer orderCount = 0;
		//订单总额(已完成)
		BigDecimal orderPriceCount = BigDecimal.ZERO;
		if (ToolUtil.isNotEmpty(orderList)) {
			orderCount = orderList.size();
			for (Order order : orderList) {
				orderPriceCount = orderPriceCount.add(ToolUtil.isNotEmpty(order.getOrderPrice()) ? order.getOrderPrice() : BigDecimal.ZERO);
			}
		}
		//待处理订单 -> 选择银行支付,且支付状态未支付的
		int toDoCount = orderService.selectCount(new EntityWrapper<Order>().eq("pay_type", SxbdConstants.ORDER_PAY_TYPE_BANK_TRANSFER)
				.eq("pay_state", SxbdConstants.ORDER_PAY_STATE_NOPAY)
				.eq("order_state", SxbdConstants.ORDER_STATE_NEW));
		//发票总数
		int billCount = billService.selectCount(new EntityWrapper<Bill>().isNotNull("order_id"));
		//待处理发票 申请发票状态(0.未开具;1已开具已寄送;2已开具未寄送;3.已驳回)
		int toDoBillCount = billService.selectCount(new EntityWrapper<Bill>().isNotNull("order_id").in("bill_state", new Integer[]{0, 2, 3}));

		model.addAttribute("orderCount", orderCount);
		//model.addAttribute("warnCount", warns.size());
		model.addAttribute("orderPriceCount", orderPriceCount);
		model.addAttribute("toDoCount", toDoCount);
		model.addAttribute("billCount", billCount);
		model.addAttribute("toDoBillCount", toDoBillCount);

		return "/blackboard.html";
	}

	@RequestMapping(value = "/warns/count")
	@ResponseBody
	public ResponseResult<Object> warns() {
		ResponseResult<Object> result = new ResponseResult<Object>();
		//报警数
		List<Map<String, Object>> warns = warnService.getWarns(null,null);
		result.setCount(warns.size());
		result.setCode(SUCCESS_TIP.getCode());
		result.setMsg(SUCCESS_TIP.getMessage());
		return result;
	}

    /**
     * 在线差分账号占比
     * @return
     */
    @RequestMapping(value = "/accountForPieChart")
    @ResponseBody
    public ResponseResult<Object> onlineAccountForPieChart() {
    	ResponseResult<Object> result = new ResponseResult<Object>();
    	List<Map<String, Object>> accountForPieChart = new ArrayList<>();
		//TODO 接口不通，使用临时数据
//    	String json = HttpUtils.get("http://49.4.94.172:9099/shxcors/account/online");
		String json = "[\"SHY001/shy001001\",\"5HY003/shy001003\",\"SHY002/shy001002\"]";
    	JSONArray jsonArray = JSONObject.parseArray(json);
    	System.out.println("在线==========="+jsonArray.size());
    	Map<String, Object> mapForOnline = new HashMap<String, Object>();
    	mapForOnline.put("name", "在线");
    	mapForOnline.put("value", jsonArray.size());
    	accountForPieChart.add(mapForOnline);
    	int accountCount = accountService.selectCount(new EntityWrapper<Account>().eq("binding_state", 1));
    	System.out.println("accountCount==========="+accountCount);
    	Map<String, Object> mapForOffline = new HashMap<String, Object>();
    	mapForOffline.put("name", "离线");
    	mapForOffline.put("value", accountCount-jsonArray.size());
    	accountForPieChart.add(mapForOffline);
        result.setData(accountForPieChart);
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }
    
    
    @RequestMapping(value = "/userForCurve")
    @ResponseBody
    public ResponseResult<Object> userForCurve(Integer type) {
    	/*ResponseResult<Object> result = new ResponseResult<Object>();
    	List<Map<String, Object>> userForCurve = userService.selectUserForCurve(year);
        result.setData(userForCurve);
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;*/
    	ResponseResult<Object> result = new ResponseResult<Object>();
		List<Map<String, Object>> userForCurve;
    	if(type == 1){
    		userForCurve = userService.selectUserForCurveByDay();
		} else if (type == 2) {
			userForCurve = userService.selectUserForCurveByMonth();
		} else if (type == 3) {
			userForCurve = userService.selectUserForCurveByYear();
		} else {
			throw new SxbdException("获取用户数据统计折线图type失败");
		}
        result.setData(userForCurve);
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

	/**
	 * 服务账号统计折线图
	 * @param type 1日2月3年
	 */
	@RequestMapping(value = "/accountForCurve")
    @ResponseBody
    public ResponseResult<Object> accountForCurve(Integer type) {
    	ResponseResult<Object> result = new ResponseResult<Object>();
		List<Map<String, Object>> accountForCurve;
    	if(type == 1){
			accountForCurve = accountService.selectAccountForCurveByDay();
		} else if (type == 2) {
			accountForCurve = accountService.selectAccountForCurveByMonth();
		} else if (type == 3) {
			accountForCurve = accountService.selectAccountForCurveByYear();
		} else {
			throw new SxbdException("获取服务账号统计折线图type失败");
		}
        result.setData(accountForCurve);
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

	/**
	 *  订单服务统计折线图 正常 已支付订单
	 * @param type 1日2月3年
	 */
	@RequestMapping(value = "/orderForCurve")
    @ResponseBody
    public ResponseResult<Object> orderForCurve(Integer type) {
    	ResponseResult<Object> result = new ResponseResult<Object>();
    	List<Map<String, Object>> orderForCurve;
		if (type == 1){
			orderForCurve = orderService.selectOrderForCurveByDay();
		} else if (type == 2) {
			orderForCurve = orderService.selectOrderForCurveByMonth();
		} else if (type == 3) {
			orderForCurve = orderService.selectOrderForCurveByYear();
		} else {
			throw new SxbdException("订单服务统计折线图type失败");
		}
        result.setData(orderForCurve);
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }
}
